package com.zhongwang.business.module.estimateBudgetAccountingManage.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.BusBudgetAccountingCost;
import com.zhongwang.business.gen.entity.BusBudgetAccountingTask;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.AccountingInfoDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.DesignWeightModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.LaborTimeModel;
import com.zhongwang.business.util.ShiroUtils;
import com.zhongwang.business.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 徐一贺
 * @className OutImportListener
 * @description 外协件信息导入监听器
 * @date 2020/11/21 14:46
 * @versron ver 1.0
 */
public class LaborImportListener extends AnalysisEventListener<LaborTimeModel> {
  /** 钩子 */
  private ImportCallBack callBack;
  /** Excel相关参数模型 */
  private BusBudgetAccountingCost param;
  /** 材料信息数据持久层 */
  private AccountingInfoDao accountingInfoDao;
  /** 错误列表 */
  private List<LaborTimeModel> errorList = new ArrayList<>();
  /** 持久化列表 */
  private List<LaborTimeModel> persistenceList = new ArrayList<>();
  /** 当前时间 */
  private Date currentTime = new Date();

  /**
   * 构造函数
   *
   * @param param 页面参数
   * @param callBack 回调函数
   * @param accountingInfoDao 持久层
   */
  public LaborImportListener(
      BusBudgetAccountingCost param, ImportCallBack callBack, AccountingInfoDao accountingInfoDao) {
    this.callBack = callBack;
    this.param = param;
    this.accountingInfoDao = accountingInfoDao;
  }

  /**
   * 验证
   *
   * @param data 每行数据
   */
  @Override
  public void invoke(LaborTimeModel data, AnalysisContext context) {
    String errorInfo = "";
    if (data.getTimeType() == null || StringUtils.trimAllBlank(data.getTimeType()).equals("")) {
      errorInfo = "请选择工时类别！\r\n";
    }else if (!data.getTimeType().equals("下料工时")
            && !data.getTimeType().equals("铆焊工时")
            && !data.getTimeType().equals("装配工时")
            && !data.getTimeType().equals("热处理工时")
            && !data.getTimeType().equals("安装工时")
            && !data.getTimeType().equals("调试工时")
            && !data.getTimeType().equals("设计工时")
            && !data.getTimeType().equals("包装工时")) {
      errorInfo = "工时类别只能是下料工时、铆焊工时、装配工时、热处理工时、安装工时、调试工时、设计工时和包装工时中的一种！\r\n";
    }
    if (data.getLaborName() == null || StringUtils.trimAllBlank(data.getLaborName()).equals("")) {
      errorInfo = "请填写工时类别！\r\n";
    }
    if (data.getLaborNum() == null) {
      errorInfo = "请填写数量！\r\n";
    }
    if (data.getUnit() == null || StringUtils.trimAllBlank(data.getUnit()).equals("")) {
      errorInfo = "请填写单位，并且单位只能是工日！\r\n";
    }else if (!data.getUnit().equals("吨")) {
      errorInfo = "单位只能填写工日！\r\n";
    }

    if (errorInfo.length() > 0) {
      errorList.add(data);
    } else {
      persistenceList.add(data);
    }
  }

  /**
   * 持久化
   *
   * @param context
   */
  @Override
  public void doAfterAllAnalysed(AnalysisContext context) {
    if(errorList.size()>0){
      callBack.onWarning(errorList);
    }
    BusBudgetAccountingTask order = accountingInfoDao.getBusBudgetAccountingTaskModelById(param.getBatId());
    for (int i = 0; i < persistenceList.size(); i++) {
      LaborTimeModel persistenceData = persistenceList.get(i);
      persistenceData.setBatId(param.getBatId());
      persistenceData.setMajor(param.getMajor());
      persistenceData.setPart(param.getPart());
      BusBudgetAccountingCost getBusBudgetAccountingCostCondition = null;
      if (persistenceData.getTimeType().equals("下料工时")){
        if (param.getMajor().equals("机械")) {
          persistenceData.setType(7);
        } else if (param.getMajor().equals("流体")) {
          persistenceData.setType(8);
        } else if (param.getMajor().equals("自动化")) {
          persistenceData.setType(9);
        }
        persistenceData.setLaborType(4);
        getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
        getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
        getBusBudgetAccountingCostCondition.setMajor(param.getMajor());
        getBusBudgetAccountingCostCondition.setPart(param.getPart());
        getBusBudgetAccountingCostCondition.setCode(param.getMajor()+"-"+param.getPart()+"-制造费-下料");
        BusBudgetAccountingCost setCostModel = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        persistenceData.setCostId(setCostModel.getId());
      }else if (persistenceData.getTimeType().equals("铆焊工时")){
        if (param.getMajor().equals("机械")) {
          persistenceData.setType(13);
        } else if (param.getMajor().equals("流体")) {
          persistenceData.setType(14);
        } else if (param.getMajor().equals("自动化")) {
          persistenceData.setType(15);
        }
        persistenceData.setLaborType(6);
        getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
        getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
        getBusBudgetAccountingCostCondition.setMajor(param.getMajor());
        getBusBudgetAccountingCostCondition.setPart(param.getPart());
        getBusBudgetAccountingCostCondition.setCode(param.getMajor()+"-"+param.getPart()+"-制造费-铆焊");
        BusBudgetAccountingCost setCostModel = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        persistenceData.setCostId(setCostModel.getId());
      }else if (persistenceData.getTimeType().equals("装配工时")){
        if (param.getMajor().equals("机械")) {
          persistenceData.setType(31);
        } else if (param.getMajor().equals("流体")) {
          persistenceData.setType(32);
        } else if (param.getMajor().equals("自动化")) {
          persistenceData.setType(33);
        }
        persistenceData.setLaborType(13);
        getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
        getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
        getBusBudgetAccountingCostCondition.setMajor(param.getMajor());
        getBusBudgetAccountingCostCondition.setPart(param.getPart());
        getBusBudgetAccountingCostCondition.setCode(param.getMajor()+"-"+param.getPart()+"-制造费-装配");
        BusBudgetAccountingCost setCostModel = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        persistenceData.setCostId(setCostModel.getId());
      }else if (persistenceData.getTimeType().equals("安装工时")){
        if (param.getMajor().equals("机械")) {
          persistenceData.setType(38);
        } else if (param.getMajor().equals("流体")) {
          persistenceData.setType(39);
        } else if (param.getMajor().equals("自动化")) {
          persistenceData.setType(40);
        }
        persistenceData.setLaborType(18);
        getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
        getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
        getBusBudgetAccountingCostCondition.setMajor(param.getMajor());
        getBusBudgetAccountingCostCondition.setPart(param.getPart());
        getBusBudgetAccountingCostCondition.setCode(param.getMajor()+"-"+param.getPart()+"-现场安装费-人工-安装人工");
        BusBudgetAccountingCost setCostModel = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        persistenceData.setCostId(setCostModel.getId());
      }else if (persistenceData.getTimeType().equals("调试工时")){
        if (param.getMajor().equals("机械")) {
          persistenceData.setType(1);
        } else if (param.getMajor().equals("流体")) {
          persistenceData.setType(2);
        } else if (param.getMajor().equals("自动化")) {
          persistenceData.setType(3);
        }
        persistenceData.setLaborType(20);
        getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
        getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
        getBusBudgetAccountingCostCondition.setMajor(param.getMajor());
        getBusBudgetAccountingCostCondition.setPart(param.getPart());
        getBusBudgetAccountingCostCondition.setCode(param.getMajor()+"-"+param.getPart()+"-现场安装费-人工-调试人工");
        BusBudgetAccountingCost setCostModel = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        persistenceData.setCostId(setCostModel.getId());
      }else if (persistenceData.getTimeType().equals("设计工时")){
        if (param.getMajor().equals("机械")) {
          persistenceData.setType(1);
        } else if (param.getMajor().equals("流体")) {
          persistenceData.setType(2);
        } else if (param.getMajor().equals("自动化")) {
          persistenceData.setType(3);
        }
        persistenceData.setLaborType(1);
        getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
        getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
        getBusBudgetAccountingCostCondition.setMajor(param.getMajor());
        getBusBudgetAccountingCostCondition.setPart(param.getPart());
        getBusBudgetAccountingCostCondition.setCode(param.getMajor()+"-"+param.getPart()+"-设计费");
        BusBudgetAccountingCost setCostModel = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        persistenceData.setCostId(setCostModel.getId());
      }
      persistenceData.setProCode(order.getProCode());
      persistenceData.setDataUseType(2);
      persistenceData.setGetDate(currentTime);
      persistenceData.setGetUid(ShiroUtils.getUserId());
      List<LaborTimeModel> existDatas = accountingInfoDao.findImportLaborTimeExistDataByCondition(persistenceData);
      if(existDatas!=null && existDatas.size()>0){
        accountingInfoDao.deleteLaborTimeModelByCostId(persistenceData.getCostId());
      }
      accountingInfoDao.insertLaborTimeImport(persistenceData);
    }
    callBack.onSuccess();
  }

  /**
   * 异常处理
   *
   * @param exception 异常信息
   * @param context
   */
  @Override
  public void onException(Exception exception, AnalysisContext context) {

  }
}
